cc.Class({
  extends: cc.Component,

  properties: {
    spacing: 20,
    cols: 0, //每行多少个
    brickPrefab: cc.Prefab,
    bricksNumber: 0
  },

  init(bricksNumber) {
    this.node.removeAllChildren();
    this.bricksNumber = bricksNumber;

    let { width, height } = this.brickPrefab.data;
    let screenWidth = this.node.width; //cc.winSize.width
    // 动态计算一行多少个
    this.cols = parseInt(screenWidth / (width + this.spacing));
    // 左右动态间距
    let interval =
      (screenWidth - this.cols * (width + this.spacing) + this.spacing) / 2;

    for (let i = 0; i < this.bricksNumber; i++) {
      let brickNode = cc.instantiate(this.brickPrefab);
      brickNode.parent = this.node;

      brickNode.x =
        +(i % this.cols) * (brickNode.width + this.spacing) +
        interval +
        brickNode.width / 2;

      brickNode.y =
        -Math.floor(i / this.cols) * (brickNode.height + this.spacing) -
        brickNode.height / 2;

      const randomInt = (min, max) =>
        Math.floor(Math.random() * (max - min) + min);

      // 目前先是随机的,后期加入关卡和难度更改
      let type = randomInt(1, 100);
      if (type <= 90) {
        type = 0;
      } else if (type <= 96) {
        type = 1;
      } else {
        type = 2;
      }

      brickNode.getComponent('brickTemplate').init({
        type: type,
        HP: randomInt(1, 10),
        icon: randomInt(0, 5) //0-5不同的的精灵
      });
    }
  }
});
